/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: com_error_domain.h
 *
 * Purpose: Any Checked Error of a service interface shall be reported via
 *          the return type as specified in
 *
 * Developer:
 *   wen.gu , 2020-04-21
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_COM_ERROR_DOMAIN_H__
#define __ARA_COM_COM_ERROR_DOMAIN_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "ara/core/error_domain.h"
#include "ara/core/exception.h"
#include "ara/core/error_code.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{
enum class ComErrc : ara::core::ErrorDomain::CodeType
{
    kServiceNotAvailable = 1,  /** Service is not available. */
    kMaxSamplesExceeded = 2,   /** Application holds more SamplePtrs than commited in Subscribe().*/
    kNetworkBindingFailure = 3, /** Local failure has been detected by the network binding.*/

    /** extend by vendor */
    kUnknownMethod = 100, /** unknown method call */
    kUnknownError = 101, /** unknown error occur */
    kInvalidOperation = 102, /** current operation(request, feild get/set and so on) is invalid */
    kFeildNoSetter = 103,  /** feild not handle set operation */
    kFeildNoGetter = 104,  /**feild not handle get operation */
    kFeildNoNotifer = 105, /** feild not handl notify operation */
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class ComException : public ara::core::Exception
{
public:
    ComException(ara::core::ErrorCode err)
        :ara::core::Exception(err)
    {
        /** todo something */
    }
};

class ComErrorDomain : public ara::core::ErrorDomain
{
    using Errc = ComErrc;
    using Exception = ComException;
public:
    constexpr ComErrorDomain(void) noexcept
        :ErrorDomain(0x8000000000000400ULL) /** todo, refine me?? */
    {
        /** todo something */
    }

    ~ComErrorDomain() = default;
public:
    char const* Name() const noexcept override { return "Core"; }
    char const* Message(ErrorDomain::CodeType errorCode) const noexcept override
    {
        char const* ret = "";
        Errc err = static_cast<Errc>(errorCode);
        switch (err)
        {
        ERR_DOMAIN_CASE(Errc::kServiceNotAvailable);
        ERR_DOMAIN_CASE(Errc::kMaxSamplesExceeded);
        ERR_DOMAIN_CASE(Errc::kNetworkBindingFailure);
        /**for vendor define error */
        ERR_DOMAIN_CASE(Errc::kUnknownMethod);
        ERR_DOMAIN_CASE(Errc::kUnknownError);
        ERR_DOMAIN_CASE(Errc::kInvalidOperation);
        ERR_DOMAIN_CASE(Errc::kFeildNoSetter);
        ERR_DOMAIN_CASE(Errc::kFeildNoGetter);
        ERR_DOMAIN_CASE(Errc::kFeildNoNotifer);
        default: break;
        }

        return ret;
    }
    void ThrowAsException(ara::core::ErrorCode const& errorCode) const noexcept(false) override
    {
        throw ComException(errorCode);
    }

public:
    static ComErrorDomain const& GetInstance()
    {
        static ComErrorDomain cem;
        return cem;
    }

    constexpr ComErrorDomain const& GetRef() const { return  *this; }
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

constexpr inline ara::core::ErrorDomain const& GetCoreErrorDomain() noexcept
{
    return ComErrorDomain::GetInstance().GetRef();
}

constexpr inline ara::core::ErrorCode MakeErrorCode(ComErrc code, ara::core::ErrorDomain::SupportDataType data) noexcept
{
    return std::move(ara::core::ErrorCode(static_cast<ara::core::ErrorDomain::CodeType>(code), GetCoreErrorDomain(), data));
}

} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_COM_ERROR_DOMAIN_H__ */

